package com.zq.assistant.ui.widget.recyclerview.adapter.cell;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.zq.assistant.ui.widget.recyclerview.RecyclerViewHolder;
import com.zq.assistant.ui.widget.recyclerview.adapter.BaseObjectRecyclerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by zhangqiang on 17-7-3.
 */

public class CellAdapter<C extends Cell> extends BaseObjectRecyclerAdapter<C,RecyclerViewHolder> {

    private Map<Integer,Integer> layoutIdViewTypeRelations = new HashMap<>();

    public CellAdapter(List<C> dataList, Context context) {
        super(dataList, context);
    }

    @Override
    public RecyclerViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType) {

        int layoutId = getLayoutIdByViewType(viewType);
        return RecyclerViewHolder.create(context,layoutId,parent);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void onBindContentViewHolder(RecyclerViewHolder holder, int position) {

        C cell = getDataAt(position);

        cell.bindData(holder);
    }

    @Override
    public int getContentItemViewType(int position) {

        C cell = getDataAt(position);
        int layoutId  = cell.getLayoutId();

        Set<Integer> layoutIdSet = layoutIdViewTypeRelations.keySet();
        if(layoutIdSet.contains(layoutId)){

            return layoutIdViewTypeRelations.get(layoutId);
        }
        int viewType = layoutIdSet.size() + 1;
        layoutIdViewTypeRelations.put(layoutId,viewType);
        return viewType;
    }

    @Override
    public void onViewRecycled(RecyclerViewHolder holder) {
        super.onViewRecycled(holder);
        int position = holder.getAdapterPosition();
        if(position == RecyclerView.NO_POSITION){
            return;
        }
        C cell = getDataAt(position);
        cell.onRecycled(holder);
    }

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if(layoutManager instanceof GridLayoutManager){

            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup oldSizeLookup = gridLayoutManager.getSpanSizeLookup();

            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {

                    if(isContentItem(position)) {

                        C cell = getDataAt(position);
                        int spanSize = cell.getSpanSize();
                        if(spanSize == Cell.FULL_SPAN){
                            return gridLayoutManager.getSpanCount();
                        }
                        return cell.getSpanSize();
                    }

                    if(oldSizeLookup != null){
                        return oldSizeLookup.getSpanSize(position);
                    }
                    return 0;
                }
            });
        }
    }

    private int getLayoutIdByViewType(int viewType){

        for (Map.Entry<Integer,Integer> entry: layoutIdViewTypeRelations.entrySet()
             ) {

            if(viewType == entry.getValue()){
                return entry.getKey();
            }
        }
        return -1;
    }
}
